
//
//  upDownButton.m
//  BigJoy
//
//  Created by Rowling on 2017/7/5.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "upDownButton.h"

@interface upDownButton()

@property (nonatomic,strong)UILabel * label;

@end

@implementation upDownButton

-(UILabel *)label {
    if (_label == nil) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 18, 18)];
        _label.layer.cornerRadius = 9;
        _label.layer.masksToBounds = YES;
        _label.backgroundColor = [UIColor whiteColor];
        _label.layer.borderWidth = 1.5;
        _label.layer.borderColor = XMColor(245, 70, 70).CGColor;
        _label.font = [UIFont fontWithName:@"PingFang-SC-Bold" size:8];
        _label.textColor = XMColor(245, 70, 70);
        _label.text = @"2";
        _label.textAlignment = 1;
    }
    return _label;
}

-(void)layoutSubviews {
    [super layoutSubviews];
//    self.imageView.y = 17;
//    self.imageView.centerX = self.centerX;
//    self.titleLabel.centerX = self.imageView.centerX;
//    self.titleLabel.y = CGRectGetMaxY(self.imageView.frame) +5;
    [self addSubview:self.label];
    self.label.x = self.width - 18;
    [self initButton:self];
    self.label.centerX = self.imageView.x +self.imageView.width;
    self.label.centerY = self.imageView.y + 2;
}

-(void)setUnReadCount:(NSInteger)unReadCount {
    _unReadCount = unReadCount;
    if (unReadCount > 0) {
        self.label.hidden = NO;
        self.label.text = [NSString stringWithFormat:@"%ld",unReadCount];
    } else {
        self.label.hidden = YES;
    }
}

-(void)initButton:(UIButton*)btn{
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height + 10,-btn.imageView.frame.size.width , 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
    [btn setImageEdgeInsets:UIEdgeInsetsMake(-15.0, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
}

@end
